// 添加图片
import {
	upload
} from 'common/path.js'
var updataImgList = function() {
	return new Promise((resolve, reject) => {
		// setTimeout(r => {
		// }, 3000)
		let that = this
		uni.chooseImage({
			count: 1,
			sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
			success: (event) => {
				uni.showLoading({
					'title': '上传中...'
				})
				uni.uploadFile({
					url: 'https://m.yipanxueyuan.com/addons/alioss/index/upload',
					filePath: event.tempFilePaths[0],
					name: 'file',
					formData: {
						token: uni.getStorageSync('token'),
					},


					success: (res) => {
						// res.data.data.url+'?x-oss-process=image/resize,l_400'
						let analysisImg = JSON.parse(res.data)
						let img = analysisImg.data.url+'?x-oss-process=image/resize,l_400'
						console.log('[封装打印]',analysisImg);
						let data = {
							code:analysisImg.code,
							msg:analysisImg.msg,
							data:{
								url:analysisImg.data.url+'?x-oss-process=image/resize,l_400'
							}
						}
						resolve(data)
						uni.hideLoading()
					},
					fail: err => {
						reject(err)
						uni.hideLoading()
						console.log(err);
					},
					complete() {
						uni.hideLoading()
					}

				})
			}
		})
	})
}
//添加视频
var upVideo = function() {
	return new Promise((resolve, reject) => {
		// uni.showLoading({
		// 	'title': '上传中...'
		// })
		// setTimeout(r => {
		// 	uni.hideLoading()
		// }, 5000)
		let that = this
		uni.chooseVideo({
			count: 1,
			sourceType: ['camera', 'album'],
			compressed: true,
			success: function(event) {
				uni.showLoading({
					'title': '上传中...'
				})
				console.log('[video]', event);
				// console.log('上传的视频====>', event.tempFilePath);


				// let tiemr = new Date();
				// let address = tiemr.getFullYear() + '' + (tiemr.getMonth() + 1) + '' + tiemr
				// 	.getDate();
				// address = '/uploads/' + address + '/';
				// var imageSrc = res.tempFilePaths[0];
				// let str = res.tempFilePaths[0].substr(res.tempFilePaths[0].lastIndexOf('.'));
				// let nameStr = address + tiemr.getTime() + str;
				// let ossUrl = 'http://xxx.xxxxxxxxxxxxxxxxxxxxxx.xxx/';



				uni.uploadFile({
					url: 'https://m.yipanxueyuan.com/addons/alioss/index/upload',
					filePath: event.tempFilePath,
					name: 'file',
					fileType: 'video',
					formData: {
						token: uni.getStorageSync('token'),
						// 'Key': 'rzuVZxnSF33nRU5PKX5duLP9OEw2D5', //OSS存储路径/图片名称 => 如：task/test.jpg 别写成 /test.jpg 会有问题
						// 'Policy': '', //后端Base64编码格式的授权策略 用于签名
						// 'OSSAccessKeyId': 'LTAI4FyF8qaGMA3ChMkF4pQ3', //OSS控制台获取
						// 'Success_Action_Status': '200', //让服务端返回200,不然，成功上传图片后阿里默认返回statusCode: 204
						// 'Signature': '', //服务端返回的签名内容
					},
					success: (res) => {
						let analysisvideo = JSON.parse(res.data)
						// let obj = {
						// 	video: event.tempFilePath,
						// 	video2: analysisvideo.data.url
						// }
						resolve(analysisvideo)
						uni.hideLoading()
					},
					fail: err => {
						reject(err)
						uni.hideLoading()
						console.log(err);
					},
					complete() {
						uni.hideLoading()
					}

				})
			}
		});



	})
}


// 公用预览图片
var preview = function(index, imglist) {
	return new Promise((resolve, reject) => {
		uni.previewImage({
				current: index,
				urls: imglist,
				longPressActions: {
					itemList: ['发送给朋友', '保存图片', '收藏'],
				}
			})
			.then((res) => {
				resolve(res)
			})
			.catch((err) => {
				reject(err)
			})
	})
}




// 公用上传语音
var upAudio = function(audioData) {
	return new Promise((resolve, reject) => {
		let that = this
		uni.showLoading({
			'title': '加载中...'
		})
		uni.uploadFile({
			url: 'https://m.yipanxueyuan.com/addons/alioss/index/upload',
			filePath: audioData,
			// filePath: 'http://app.tiantai.com.cn/uploads/20200819/7103e314a4c998dcc5e2a75816d5bc4e.mp3',
			name: 'file',
			fileType: 'audio',
			formData: {
				token: uni.getStorageSync('token'),
			},
			success: (res) => {
				let analysisvideo = JSON.parse(res.data)
				resolve(analysisvideo)
				uni.hideLoading()
			},
			file: (err) => {
				reject(err)
				uni.hideLoading()
			},
			complete() {
				uni.hideLoading()
			}
		})
	})
}





export {
	updataImgList,
	preview,
	upVideo,
	upAudio
}
